// import axios from 'axios'

// const baseURL = 'http://a.tuiyes.com';
// const baseURL = 'http://mohe.baomihua.com';
// var baseURL = '/';
// const baseURL = 'http://mohe.pxtadmin.com';
// const baseURL = 'http://192.168.30.18:9002';
// const baseURL = 'http://192.168.30.98:9002';
// const baseURL = 'http://192.168.30.12:9002';

const baseURL = 'http://139.196.252.106/';
// const SURL = 'http://192.168.30.98:9002';
//  const imgURL = 'http://mohe.pxtadmin.com';

axios.defaults.baseURL = baseURL;

var cCookie = {// 封装原生cookie
	get:function(name){
			// 获取所有
			var cookies = document.cookie;
			// 转成数组
			cookies = cookies.split('; ');//['goodslist=[]','your=lemon']
			var res = '';
			for(var i=0;i<cookies.length;i++){
					// 拆分cookieName和cookieValue
					var arr = cookies[i].split('=');
					if(arr[0] === name){
							res = arr[1];
							break;
					}
			}
			return res;
	},
	set:function(name,value,param){
			var cookie = name + '=' + value;
			if(param){
					if(param.expires){
							cookie += ';expires='+param.expires.toUTCString();
					}
					if(param.path){
							cookie += ';path=' + param.path;
					}
					if(param.domain){
							cookie += ';domain=' + param.domain;
					}
					if(param.secure){
							cookie += ';secure'
					}
			}		
			document.cookie = cookie;
	},
	remove:function(name,path){
			// 原理：设置过期时间达到删除效果
			var now = new Date();
			now.setDate(now.getDate()-1);
			this.set(name,null,{expires:now,path:path});
	}

};
axios.defaults.withCredentials = false;
axios.defaults.headers.post['Content-Type'] = 'application/json';
var accessToken = cCookie.get('accessToken');
accessToken = 'bearer '+accessToken;
axios.defaults.headers.common['Authorization'] = accessToken;

import {Message} from 'element-ui';
import { head } from 'shelljs';
//接收响应拦截器

axios.interceptors.response.use(response => {
	if(response.data.code != 0){ 
		Message({
            title:'警告',
            message:response.data.message,
            type:'warning'
        });
    };
    // if(response.data.code == 1 && response.data.message != '' && response.data.message!=undefined){
    //     Message({
    //         title:'警告',
    //         message:response.data.message,
    //         type:'warning'
    //     });
    // };
    if(response.data.code == 30005){
        Message({
            title:'警告',
            message:response.data.message,
            type:'warning'
        });
    };
    if(response.data.code == 90001){
        var timer = null;
        timer = setInterval(function(){
            if(document.querySelector('.lookScreen')){
                clearInterval(timer);
                document.querySelector('.lookScreen').style.display = 'block';
            }
        },100);
    };
	return response
  }, err => {
	if (err && err.response) {
	  switch (err.response.status) {
		case 400: err.message = '请求错误(400)'; break;
		case 401: err.message = '请求失败(401)'; break;
		case 403: err.message = '拒绝访问(403)'; break;
		case 404: err.message = '请求出错(404)'; break;
		case 408: err.message = '请求超时(408)'; break;
		case 500: err.message = '服务器错误(500)'; break;
		case 501: err.message = '服务未实现(501)'; break;
		case 502: err.message = '网络错误(502)'; break;
		case 503: err.message = '服务不可用(503)'; break;
		case 504: err.message = '网络超时(504)'; break;
		case 505: err.message = 'HTTP版本不受支持(505)'; break;
		default: err.message = `连接出错(${err.response.status})!`;
	  }
	} else {
	  err.message = '连接服务器失败!'
	}
	Message({
        title:'警告',
        message:err.message,
        type:'warning'
    });
	return Promise.reject(err);
});
export const login = params => {//首页概览
    return axios.post('http://139.196.252.106:8001/userInfo/login', params).then(res => res.data)
}
export const userInfolist = params => {//首页概览
	var parastr = '';
	parastr+=params.params.page;
	parastr+=('/'+params.params.limit);
    return axios.get('http://139.196.252.106:8001/userInfo/list/'+parastr, params).then(res => res.data)
}
export const userInfosave = params => {
    return axios.post('http://139.196.252.106:8001/userInfo/save', params).then(res => res.data)
}
export const userInforemove = params => {
    return axios.delete('http://139.196.252.106:8001/userInfo/remove/'+params.params.id, params).then(res => res.data)
}
export const userInfoupdate = params => {
    return axios.post('http://139.196.252.106:8001/userInfo/update/'+JSON.parse(params).id, params).then(res => res.data)
}
export const updatePassword = params => {
    return axios.post('http://139.196.252.106:8001/userInfo/updatePassword/'+JSON.parse(params).id, params).then(res => res.data)
}
export const userUseTime = params => {//首页概览
	var parastr = '';
	parastr+=params.params.page;
	parastr+=('/'+params.params.limit);
    return axios.get('http://139.196.252.106:8001/userUseTime/list/'+parastr, params).then(res => res.data)
}